import { msg } from '../../components/notice/notice';

Template.delete_modal.helpers({
  title: function () {
    return Session.get('class.deleteModal.title');
  }
});

Template.delete_modal.events({
  'click #delete-modal-confirm' (e, instance) {
    const type = Session.get('class.deleteModal.type');
    const id = Session.get('class.deleteModal.id');

    if (type == 'delete-channel-message') {
      Meteor.call('deleteChannelMessage', { id }, (err, res) => {
        if (res) {
          $('#delete-modal').modal('hide');
        }
      });
    }

    if (type == 'delete-friend') {
      Meteor.call('deleteFriend', { id }, (err, res) => {
        if (res) {
          $('#delete-modal').modal('hide');
        }
      });
    }

    if (type == 'quit-channel') {
      Meteor.call('quitChannel', { id }, (err, res) => {
        if (res) {
          $('#delete-modal').modal('hide');
          msg({content: '成功退出频道！'});
          Router.go('home');
        }
      });
    }

    if (type == 'delete-channel') {
      $('#delete-modal').modal('hide');
      Meteor.call('deleteChannel', id, (err, res) => {
        if (res) {
          msg({content: '成功删除频道'});
          Router.go('home');
        }
      });
    }
  }
});
